package com.rao.crm.service.impl;

import com.rao.crm.mapper.BaseDictMapper;
import com.rao.crm.mapper.CustomerMapper;
import com.rao.crm.pojo.BaseDict;
import com.rao.crm.pojo.Customer;
import com.rao.crm.pojo.QueryVo;
import com.rao.crm.service.BaseDictService;
import com.rao.crm.service.CustomerService;
import com.rao.crm.utils.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author Srao
 * @version 1.0
 * @className CustomerServiceImpl
 * @date 2019/10/6 15:52
 * @package com.rao.crm.service.impl
 * @description
 */
@Service
public class CustomerServiceImpl implements CustomerService {

    @Autowired
    private CustomerMapper customerMapper;
    @Autowired
    private BaseDictMapper baseDictMapper;

    @Override
    public Page<Customer> findCustomerByQueryVo(QueryVo queryVo) {
        Page<Customer> page = new Page<>();
        if (null != queryVo){
            queryVo.setStart((queryVo.getPage() - 1) * queryVo.getRows());

            page.setTotal(customerMapper.findTotalByQueryVo(queryVo));
            List<Customer> customers = customerMapper.findCustomerByQueryVo(queryVo);
            customers.forEach(customer -> {
                customer.setCust_source(baseDictMapper.findBaseDictById(customer.getCust_source()).getDict_item_name());
                customer.setCust_industry(baseDictMapper.findBaseDictById(customer.getCust_industry()).getDict_item_name());
                customer.setCust_level(baseDictMapper.findBaseDictById(customer.getCust_level()).getDict_item_name());
            });
            page.setRows(customers);
            page.setPage(queryVo.getPage());
            page.setSize(queryVo.getRows());
        }
        return page;
    }

    @Override
    public Customer findCustomerById(Long id) {
        return customerMapper.findCustomerById(id);
    }

    @Override
    public void updateCustomerById(Customer customer) {
        customerMapper.updateCustomerById(customer);
    }

    @Override
    public void deleteCustomerById(Long id) {
        customerMapper.deleteCustomerById(id);
    }

    @Override
    public void addCustomer(Customer customer) {
        customerMapper.addCustomer(customer);
    }
}
